package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.controller.domain.UserRequest;
import com.example.entity.Account;
import com.example.service.AdminService;
import com.example.service.UserService;
import com.example.utils.SensitiveWordsInit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 基础前端接口
 */
@RestController
public class WebController {

    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;

    @GetMapping("/")
    public Result hello() {
        return Result.success("访问成功");
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserRequest userRequest) {
        if (ObjectUtil.isEmpty(userRequest.getUsername()) || ObjectUtil.isEmpty(userRequest.getPassword())
                || ObjectUtil.isEmpty(userRequest.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        Account account;
        if (RoleEnum.ADMIN.name().equals(userRequest.getRole())) {
            account = adminService.login(userRequest);
        } else if (RoleEnum.USER.name().equals(userRequest.getRole())) {
            account = userService.login(userRequest);
        } else {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return Result.success("登录成功",account);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRequest userRequest) {
        if (StrUtil.isBlank(userRequest.getUsername()) || StrUtil.isBlank(userRequest.getPassword())
                || ObjectUtil.isEmpty(userRequest.getRole())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.USER.name().equals(userRequest.getRole())) {
            userService.register(userRequest);
        } else {
            return Result.error(ResultCodeEnum.PARAM_ERROR);
        }
        return Result.success("注册成功");
    }

    /**
     * 修改密码
     */
    @PutMapping("/password/change")
    public Result updatePassword(@RequestBody UserRequest userRequest) {
        if (StrUtil.isBlank(userRequest.getPassword())
                || ObjectUtil.isEmpty(userRequest.getNewPassword())) {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        if (RoleEnum.ADMIN.name().equals(userRequest.getRole())) {
            adminService.updatePassword(userRequest);
        } else if (RoleEnum.USER.name().equals(userRequest.getRole())) {
            userService.updatePassword(userRequest);
        } else {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return Result.success("修改密码成功");
    }

    @PutMapping("/password/reset")
    public Result resetPassword(@RequestBody UserRequest userRequest) {
        if (RoleEnum.USER.name().equals(userRequest.getRole())) {
            userService.resetPassword(userRequest);
        } else {
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return Result.success("重置密码为123456");
    }
    @PostMapping("/loginByEmailCode")
    public Result loginByEmailCode(@RequestBody UserRequest userRequest){
        Account account;
        if(RoleEnum.USER.name().equals(userRequest.getRole())){
            account=userService.loginByEmailCode(userRequest);
        }else{
            return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        return Result.success("登录成功",account);
    }

    @GetMapping("/email")
    public Result email(@RequestParam String email, @RequestParam String type) {
        userService.sendEmail(email, type);
        return Result.success("发送邮箱验证码成功");
    }
    @PostMapping("/setSensitiveWords")
    public Result setSensitiveWords(){
        SensitiveWordsInit.readSensitiveWordsFile();
        return Result.success("上传敏感词成功");
    }


}
